//
//  RootTabBarViewController.m
//  测试111
//
//  Created by Burn on 2018/10/18.
//  Copyright © 2018年 buren. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "RootNavViewController.h"
#import "TabbarItemPopoverView.h"
@interface RootTabBarViewController ()<UITabBarControllerDelegate,TabbarItemPopoverViewDelegate>

@property(strong,nonatomic) TabbarItemPopoverView *popover;

/*tabBarItems 获取Frame时用到*/
@property(copy,nonatomic) NSMutableArray *tabbarItems;

/*titile数组二位*/
@property(strong,nonatomic) NSMutableArray *titles;

/**记录当前选中的Index*/
@property(assign,nonatomic) NSInteger currentIndex;

/**未来得及初始化的控制器可以访问这个属性获取index*/
@property(assign,nonatomic) NSInteger index;

@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    self.currentIndex = 100;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenPopover) name:@"HiddenPopover" object:nil];
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    //  第二次点击时先隐藏
    if (self.popover) {
        [self.popover removeFromSuperview];
        self.popover = nil;
    }
    
    //  获取点击了第几个TabBarItem
    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];
    
    //  正在显示状态下点击同一个TabBarItem 就 Return
    if (self.currentIndex == index) {
        self.currentIndex = 100;
        return NO;
    }
    
    
    //  tabbarItem是没有Frame的所以 转成View取出Frame
    UIView *view = (UIView *)self.tabbarItems[index];
    CGRect frame = view.frame;
    
    //  取出Popover上要显示的文字
    NSArray <NSString *>*title = self.titles[index];
    
    //  先计算一下文字长度和占据宽度
    NSString *maxTitle = title[0].length > title[1].length ? title[0] : title[1];

    CGFloat width = 0.0;
    CGFloat x = 0.0;
    //  最大文字宽高
    CGSize titleSize = [self getTextSize:maxTitle];
    
    width = frame.size.width > titleSize.width ? frame.size.width : titleSize.width;
    x = frame.size.width < titleSize.width ? frame.origin.x - (titleSize.width - frame.size.width) / 2 : frame.origin.x;
    
    //  创建Popover
    self.popover = [[TabbarItemPopoverView alloc]initWithFrame:CGRectMake(x, [UIScreen mainScreen].bounds.size.height - tabBarController.tabBar.frame.size.height - 86, width, 85) andTitle:title[0] andBottomTitle:title[1]];
    self.popover.index = index;
    
    self.popover.delegate = self;
    //  添加
    [self.view addSubview:self.popover];
    
    //  动画
    [self.popover animation];
    
    //  记录当前Index
    self.currentIndex = index;
    
    /*-----------调整TabBarItem状态------------*/
    
//    RootNavViewController *rootNav = (RootNavViewController *)viewController;


    return NO;
}

#pragma mark - 点击了地级个Item的第几次按钮
- (void)selecteItemIndex:(NSInteger)itemIndex buttonIndex:(NSInteger)btnIndex{
    self.currentIndex = 100;
    self.selectedIndex = itemIndex;
    self.index = btnIndex;
    //  当前想到的办法就是通知,通知页面刷新数据....
    [[NSNotificationCenter defaultCenter]postNotificationName:@"CHANGECONTENT" object:nil userInfo:@{@"itemIndex" : @(itemIndex),@"btnIndex" : @(btnIndex)}];
}


#pragma mark - 隐藏Popover
- (void)hiddenPopover{
    if (self.popover) {
        [self.popover hiddenPopoer];
        self.currentIndex = 100;
    }
}


- (CGSize )getTextSize:(NSString *)text{
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:12]};
    //宽高限制，用于计算文本绘制时占据的矩形块。
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    size.width = size.width + 5;
    return size;
}

- (NSMutableArray *)tabbarItems{
    if (!_tabbarItems) {
        _tabbarItems = [self.tabBar.subviews mutableCopy];
        [_tabbarItems removeObjectAtIndex:0];
    }
    return _tabbarItems;
}

- (NSMutableArray *)titles{
    if (!_titles) {
        _titles = [NSMutableArray array];
        NSArray *elementOne = @[@"提交纠纷",@"在线沟通"];
        NSArray *elementTwo = @[@"线上[律师/调解员]",@"普法直播"];
        NSArray *elementThree = @[@"[律师/调解员]登记",@"个人中心"];
        [_titles addObject:elementOne];
        [_titles addObject:elementTwo];
        [_titles addObject:elementThree];
    }
    return _titles;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end



